home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / perl5 / Debconf / FrontEnd / Passthrough.pm < prev    next >
Encoding:
Perl POD Document  |  2009-03-24  |  6.3 KB  |  289 lines

  1. #!/usr/bin/perl -w
  2. # This file was preprocessed, do not edit!
  3.  
  4.  
  5. package Debconf::FrontEnd::Passthrough;
  6. use strict;
  7. use Carp;
  8. use IO::Socket;
  9. use IO::Handle;
  10. use Debconf::FrontEnd;
  11. use Debconf::Element;
  12. use Debconf::Element::Select;
  13. use Debconf::Element::Multiselect;
  14. use Debconf::Log qw(:all);
  15. use Debconf::Encoding;
  16. use base qw(Debconf::FrontEnd);
  17.  
  18. my ($READFD, $WRITEFD, $SOCKET);
  19. if (defined $ENV{DEBCONF_PIPE}) {
  20.         $SOCKET = $ENV{DEBCONF_PIPE};
  21. } elsif (defined $ENV{DEBCONF_READFD} && defined $ENV{DEBCONF_WRITEFD}) {
  22.         $READFD = $ENV{DEBCONF_READFD};
  23.         $WRITEFD = $ENV{DEBCONF_WRITEFD};
  24. } else {
  25.         die "Neither DEBCONF_PIPE nor DEBCONF_READFD and DEBCONF_WRITEFD were set\n";
  26. }
  27.  
  28.  
  29. sub init {
  30.     my $this=shift;
  31.  
  32.         if (defined $SOCKET) {
  33.                 $this->{readfh} = $this->{writefh} = IO::Socket::UNIX->new(
  34.                 Type => SOCK_STREAM,
  35.                 Peer => $SOCKET
  36.             ) || croak "Cannot connect to $SOCKET: $!";
  37.         } else {
  38.                 $this->{readfh} = IO::Handle->new_from_fd(int($READFD), "r") || croak "Failed to open fd $READFD: $!";
  39.                 $this->{writefh} = IO::Handle->new_from_fd(int($WRITEFD), "w") || croak "Failed to open fd $WRITEFD: $!";
  40.         }
  41.  
  42.     binmode $this->{readfh}, ":utf8";
  43.     binmode $this->{writefh}, ":utf8";
  44.  
  45.     $this->{readfh}->autoflush(1);
  46.     $this->{writefh}->autoflush(1);
  47.     
  48.     $this->elements([]);
  49.     $this->interactive(1);
  50.     $this->need_tty(0);
  51. }
  52.  
  53.  
  54. sub talk {
  55.     my $this=shift;
  56.     my $command=join(' ', map { Debconf::Encoding::to_Unicode($_) } @_);
  57.     my $reply;
  58.     
  59.     my $readfh = $this->{readfh} || croak "Broken pipe";
  60.     my $writefh = $this->{writefh} || croak "Broken pipe";
  61.     
  62.     debug developer => "----> $command";
  63.     print $writefh $command."\n";
  64.     $writefh->flush;
  65.     $reply = <$readfh>;
  66.     chomp($reply);
  67.     debug developer => "<---- $reply";
  68.     my ($tag, $val) = split(' ', $reply, 2);
  69.     $val = '' unless defined $val;
  70.     $val = Debconf::Encoding::convert("UTF-8", $val);
  71.  
  72.     return ($tag, $val) if wantarray;
  73.     return $tag;
  74. }
  75.  
  76.  
  77. sub makeelement
  78. {
  79.     my $this=shift;
  80.     my $question=shift;
  81.  
  82.     my $type=$question->type;
  83.     if ($type eq "select" || $type eq "multiselect") {
  84.         $type=ucfirst($type);
  85.         return "Debconf::Element::$type"->new(question => $question);
  86.     } else {
  87.         return Debconf::Element->new(question => $question);
  88.     }
  89. }
  90.  
  91.  
  92. sub capb_backup
  93. {
  94.     my $this=shift;
  95.     my $val = shift;
  96.  
  97.     $this->{capb_backup} = $val;
  98.     $this->talk('CAPB', 'backup') if $val;
  99. }
  100.  
  101.  
  102. sub capb
  103. {
  104.     my $this=shift;
  105.     my $ret;
  106.     return $this->{capb} if exists $this->{capb};
  107.  
  108.     ($ret, $this->{capb}) = $this->talk('CAPB');
  109.     return $this->{capb} if $ret eq '0';
  110. }
  111.  
  112.  
  113. sub title
  114. {
  115.     my $this = shift;
  116.     return $this->{title} unless @_;
  117.     my $title = shift;
  118.  
  119.     $this->{title} = $title;
  120.     $this->talk('TITLE', $title);
  121. }
  122.  
  123.  
  124. sub settitle
  125. {
  126.     my $this = shift;
  127.     my $question = shift;
  128.  
  129.     $this->{title} = $question->description;
  130.  
  131.     my $tag = $question->template->template;
  132.     my $type = $question->template->type;
  133.     my $desc = $question->description;
  134.     my $extdesc = $question->extended_description;
  135.  
  136.     $this->talk('DATA', $tag, 'type', $type);
  137.  
  138.     if ($desc) {
  139.         $desc =~ s/\n/\\n/g;
  140.         $this->talk('DATA', $tag, 'description', $desc);
  141.     }
  142.  
  143.     if ($extdesc) {
  144.         $extdesc =~ s/\n/\\n/g;
  145.         $this->talk('DATA', $tag, 'extended_description', $extdesc);
  146.     }
  147.  
  148.     $this->talk('SETTITLE', $tag);
  149. }
  150.  
  151.  
  152. sub go {
  153.     my $this = shift;
  154.  
  155.     my @elements=grep $_->visible, @{$this->elements};
  156.     foreach my $element (@elements) {
  157.         my $question = $element->question;
  158.         my $tag = $question->template->template;
  159.         my $type = $question->template->type;
  160.         my $desc = $question->description;
  161.         my $extdesc = $question->extended_description;
  162.         my $default;
  163.         if ($type eq 'select') {
  164.             $default = $element->translate_default;
  165.         } elsif ($type eq 'multiselect') {
  166.             $default = join ', ', $element->translate_default;
  167.         } else {
  168.             $default = $question->value;
  169.         }
  170.  
  171.                 $this->talk('DATA', $tag, 'type', $type);
  172.  
  173.         if ($desc) {
  174.             $desc =~ s/\n/\\n/g;
  175.             $this->talk('DATA', $tag, 'description', $desc);
  176.         }
  177.  
  178.         if ($extdesc) {
  179.             $extdesc =~ s/\n/\\n/g;
  180.             $this->talk('DATA', $tag, 'extended_description',
  181.                         $extdesc);
  182.         }
  183.  
  184.         if ($type eq "select" || $type eq "multiselect") {
  185.             my $choices = $question->choices;
  186.             $choices =~ s/\n/\\n/g if ($choices);
  187.             $this->talk('DATA', $tag, 'choices', $choices);
  188.         }
  189.  
  190.         $this->talk('SET', $tag, $default) if $default ne '';
  191.  
  192.         my @vars=$Debconf::Db::config->variables($question->{name});
  193.         for my $var (@vars) {
  194.             my $val=$Debconf::Db::config->getvariable($question->{name}, $var);
  195.             $val='' unless defined $val;
  196.             $this->talk('SUBST', $tag, $var, $val);
  197.         }
  198.  
  199.         $this->talk('INPUT', $question->priority, $tag);
  200.     }
  201.  
  202.     if (@elements && (scalar($this->talk('GO')) eq "30") && $this->{capb_backup}) {
  203.         return;
  204.     }
  205.     
  206.     foreach my $element (@{$this->elements}) {
  207.         if ($element->visible) {
  208.             my $tag = $element->question->template->template;
  209.             my $type = $element->question->template->type;
  210.  
  211.             my ($ret, $val)=$this->talk('GET', $tag);
  212.             if ($ret eq "0") {
  213.                 if ($type eq 'select') {
  214.                     $element->value($element->translate_to_C($val));
  215.                 } elsif ($type eq 'multiselect') {
  216.                     $element->value(join(', ', map { $element->translate_to_C($_) } split(', ', $val)));
  217.                 } else {
  218.                     $element->value($val);
  219.                 }
  220.                 debug developer => "Got \"$val\" for $tag";
  221.             }
  222.         } else {
  223.             $element->show;
  224.         }
  225.     }
  226.  
  227.     return 1;
  228. }
  229.  
  230.  
  231. sub progress_data {
  232.     my $this=shift;
  233.     my $subcommand=shift;
  234.     my $question=shift;
  235.  
  236.     my $tag=$question->template->template;
  237.     my $type=$question->template->type;
  238.     my $desc=$question->description;
  239.     my $extdesc=$question->extended_description;
  240.  
  241.     $this->talk('DATA', $tag, 'type', $type);
  242.  
  243.     if ($desc) {
  244.         $desc =~ s/\n/\\n/g;
  245.         $this->talk('DATA', $tag, 'description', $desc);
  246.     }
  247.  
  248.     if ($extdesc) {
  249.         $extdesc =~ s/\n/\\n/g;
  250.         $this->talk('DATA', $tag, 'extended_description', $extdesc);
  251.     }
  252. }
  253.  
  254. sub progress_start {
  255.     my $this=shift;
  256.  
  257.     $this->progress_data('START', $_[2]);
  258.     return $this->talk('PROGRESS', 'START', $_[0], $_[1], $_[2]->template->template);
  259. }
  260.  
  261. sub progress_set {
  262.     my $this=shift;
  263.  
  264.     return (scalar($this->talk('PROGRESS', 'SET', $_[0])) ne "30");
  265. }
  266.  
  267. sub progress_step {
  268.     my $this=shift;
  269.  
  270.     return (scalar($this->talk('PROGRESS', 'STEP', $_[0])) ne "30");
  271. }
  272.  
  273. sub progress_info {
  274.     my $this=shift;
  275.  
  276.     $this->progress_data('INFO', $_[0]);
  277.     return (scalar($this->talk('PROGRESS', 'INFO', $_[0]->template->template)) ne "30");
  278. }
  279.  
  280. sub progress_stop {
  281.     my $this=shift;
  282.  
  283.     return $this->talk('PROGRESS', 'STOP');
  284. }
  285.  
  286.  
  287. 1
  288.  
  289.